using CriWare.CriTimeline.Mana;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

namespace IQIGame.Onigao.GamePlay
{
    public class CriwareVideoMixedBehaviour : CriManaMixerBehaviour
    {
        public override void ProcessFrame(Playable playable, FrameData info, object playerData)
        {
            double frameTime = m_PlayableDirector.time;
            TimelineClip activeClip = null;
            foreach (var clip in m_clips)
            {
                if (frameTime >= clip.start && frameTime < clip.end)
                {
                    activeClip = clip;
                }
                else if (frameTime >= clip.start - cPreloadTimeSec && frameTime <= clip.start)
                {
                    activeClip = clip;
                }
            }
            if (m_movieMixerState == MovieMixerState.Playing && activeClip != null)
            {
                CriManaClipBase clipAsset = activeClip.asset as CriManaClipBase;
                if (clipAsset is CriwareVideoClip rVideoClip && rVideoClip.alpha != null)
                {
                    var fAlpha = rVideoClip.alpha.Evaluate((float)(frameTime - activeClip.start));
                    CanvasGroup rRawAlpha = null;
#if UNITY_EDITOR
                    if (BattleClientUtil.IsEditorMode)
                    {
                        rRawAlpha = SkillEditorMainUpdate.Instance.moviePanel;
                    }
                    else
#endif
                    {
                        rRawAlpha = CommonVideoPlayManager.Instance.rawAlpha;
                    }
                    if (rRawAlpha != null && fAlpha > 0)
                    {
                        rRawAlpha.alpha = fAlpha;
                    }
                }
            }
            base.ProcessFrame(playable, info, playerData);
        }

        protected override void ProcessFrameOnSeeking(TimelineClip activeClip, CriManaClipBase clip, double frameTime)
        {
            base.ProcessFrameOnSeeking(activeClip, clip, frameTime);
            if (clip is CriwareVideoClip rVideoClip && rVideoClip.alpha != null)
            {
                var fAlpha = rVideoClip.alpha.Evaluate((float)(frameTime - activeClip.start));
                CanvasGroup rRawAlpha;
#if UNITY_EDITOR
                if (BattleClientUtil.IsEditorMode)
                {
                    rRawAlpha = SkillEditorMainUpdate.Instance.moviePanel;
                }
                else
#endif
                {
                    rRawAlpha = CommonVideoPlayManager.Instance.rawAlpha;
                }
                rRawAlpha.alpha = fAlpha;
            }
        }
    }
}